import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yysk_app/model/pay_model/bill_page_model.dart';
import 'package:yysk_app/model/pay_model/page_with_draw_list_model.dart';
import 'package:yysk_app/net/api/pay_url.dart';
import 'package:yysk_app/net/http.dart';

class WithdrawRecordModel {
  final String status;
  final String time;
  final double amount;
  WithdrawRecordModel({
    required this.status,
    required this.time,
    required this.amount,
  });
}

class WithdrawRecordController extends GetxController {
  var records = <WithdrawRecordModel>[].obs;
  var withDrawRecordList = RxList<PathWithDrawListItemModel>([]);
  final ScrollController scrollController = ScrollController();
  int pageNum = 1;
  int pageSize = 20;
  @override
  void onInit() {
    super.onInit();
    getWithdrawRecord();
    scrollController.addListener(() {
      if (scrollController.position.pixels ==
          scrollController.position.maxScrollExtent) {
        getWithdrawRecord(loadMore: true);
      }
    });
  }

  Future<void> getWithdrawRecord({loadMore = false}) async {
    if (loadMore) {
      pageNum++;
    } else {
      pageNum = 1;
    }

    var params = {'pageNum': pageNum, 'pageSize': pageSize};

    try {
      await http.netFetch<PathWithDrawListModel>(
        PayUrl.pageWithdraw,
        params: params,
        success: (result) {
          List<PathWithDrawListItemModel> list = result.data?.list ?? [];
          if (loadMore && list.isEmpty) {
            pageNum--;
          }
          if (!loadMore) {
            withDrawRecordList.clear();
          }
          withDrawRecordList.addAll(list);
        },
      );
    } catch (e) {
      debugPrint(e.toString());
      if (loadMore) {
        pageNum--;
      }
    }
  }
}
